package com.ats.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ats.model.Instructor;
import com.ats.model.Student;
import com.ats.model.Teacher;
import com.ats.model.User;
import com.ats.service.InstructorService_xh;
import com.ats.service.StudentService_xh;
import com.ats.service.TeacherService_xh;
import com.ats.service.UserSerivce_xh;
import com.ats.serviceimpl.InstructorServiceImpl_xh;
import com.ats.serviceimpl.StudentServiceImpl_xh;
import com.ats.serviceimpl.TeacherServiceImpl_xh;
import com.ats.serviceimpl.UserServiceImpl_xh;

import net.sf.json.JSONObject;
/**
 * 个人信息修改
 * @author xh
 *
 */
public class UpdPerController_xh extends HttpServlet{
	UserSerivce_xh us = new UserServiceImpl_xh();
	InstructorService_xh ins = new InstructorServiceImpl_xh();
	StudentService_xh stus = new StudentServiceImpl_xh();
	TeacherService_xh teas = new TeacherServiceImpl_xh();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session =req.getSession();
		PrintWriter pw =resp.getWriter();
		int uid =Integer.parseInt(req.getParameter("uid"));	
		//获取用户权限
		int ustate =us.getUstateByUid(uid);
		String jso =req.getParameter("jso");
		JSONObject jsob=JSONObject.fromObject(jso);	
		//根据用户权限进行判断
		if(ustate==0){
			//修改辅导员信息
			int iid =((User)session.getAttribute("user")).getIid();
			Instructor instr =(Instructor)JSONObject.toBean(jsob, Instructor.class);
			boolean flag =ins.updateInstructorByIid(
					instr.getName(), instr.getAge(), instr.getAddress(), instr.getNumber(), iid);
			pw.write(flag+"");
		
		}
		else if(ustate==1){
			//修改老师信息
			int tid =((User)session.getAttribute("user")).getTid();
			Teacher te =(Teacher) JSONObject.toBean(jsob,Teacher.class);
			boolean flag =teas.updateTeacherByTid(
					te.getName(), te.getAge(), te.getAddress(), te.getNumber(), tid);
			pw.write(flag+"");
		}
		else if(ustate==2){
			//修改学生信息
			int sid =((User)session.getAttribute("user")).getSid();
			Student st =(Student) JSONObject.toBean(jsob,Student.class);
			boolean flag =stus.updateStudentBySid(
					st.getName(), st.getAge(), st.getAddress(), st.getNumber(), sid);
			pw.write(flag+"");
		}
		pw.flush();
		pw.close();	
	}
	
	
	
}
